package com.beibojd.controller;


import com.beibojd.pojo.*;
import com.beibojd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 洪耀荣
 * @Date: 2021-06-23 18:07:13
 */
@RequestMapping("/")
@Controller
public class WebController {
    /**
     * 与service连接的部分
     */
    @Autowired
    private AdminService adminService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderAddService orderAddService;

    /**
     * 各个子网页的跳转
     * @return
     */
    @RequestMapping(value = "/index0")
    public String index(){
        return "index0";
    }
    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/welcome")
    public String welcome(){
        return "welcome";
    }
    @RequestMapping(value = "/customerAdd")
    public String customerAdd(){
        return "/customerAdd";
    }
    @RequestMapping(value = "/goodsAdd")
    public String goodsAdd(){return "/goodsAdd";}


    /**
     * 删除商品的逻辑：
     * 调用service删除后再返回list页面
     * ****6.26修：
    * 此处返回是不触发获取数值的，
     * 所以在service处去查询全部返回回来，
     * 再给list返回就能解决原本直接返回list页无数据的问题
     * ***
     *
     */
    @RequestMapping(value = "/delGoods")
    public String goodsDel(@RequestParam(value = "goodsid")Integer goodsid,
                           ModelMap modelMap){
        //删除
        goodsService.delgood(goodsid);
        //解决list页无数据问题
        modelMap.addAttribute("Goods",goodsService.selectAllGoods());
        return "goodsList";
    }

    /**
     * 删除客户
     * @param customerid
     * @return
     */
    @RequestMapping(value = "/delCustomer")
    public String customerDel(@RequestParam(value = "customerid") Integer customerid,
                              ModelMap modelMap){
        customerService.delCustomer(customerid);
        List<Customer> customers=customerService.selectAllCustomer();
        modelMap.addAttribute("aa",customers);
        System.out.println(customers);
        return "customerList";
    }

    /**
     * 进入用户列表时，自动查询所有的用户
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/customerList")
    public String customerList(ModelMap modelMap){

        List<Customer> customers=customerService.selectAllCustomer();
        modelMap.addAttribute("aa",customers);
        System.out.println(customers);
        return "customerList";
    }
    /**
     * 进入goods页面时展示所有商品
     */
    @RequestMapping(value = "/allGoods")
    public String allGoods(ModelMap modelMap){
        List<Goods> goods=goodsService.selectAllGoods();
        modelMap.addAttribute("Goods",goods);
        System.out.println(goods);
        return "goodsList";
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/ulogin")
    public String logintest(@RequestParam(value = "username",required = false) String username,
                            @RequestParam(value = "password",required = false) String password){
        //在控制台打出输入，修bug的时候用
        System.out.println(username);
        System.out.println(password);
        Admin admin=new Admin(username,password);
        if (adminService.selectAdmin(admin)){
            return "index";
        }else {
            return "login";
        }
    }

    /**
     * 添加客户
     * @param customerName
     * @param customerTel
     * @param customerAddress
     * @param customerTel2
     * @param customerTransport
     * @return
     */
    @RequestMapping(value = "/addCustomer")
    public String addCustomer(@RequestParam(value = "customerName") String customerName,
                              @RequestParam(value = "customerTel") String customerTel,
                              @RequestParam(value = "customerAddress") String customerAddress,
                              @RequestParam(value = "customerTel2") String customerTel2,
                              @RequestParam(value = "customerTransport") String customerTransport){
        Customer customer=new Customer(customerName,customerTel,customerTel2,customerAddress,customerTransport);
        System.out.println(customer.toString());
        Result result= customerService.addCustomer(customer);
        System.out.println(result);
        return "customerAdd";
    }

    /**
     * 添加商品
     * @param goodsName
     * @param goodsFactory
     * @param goodsSpecs
     * @param goodsModel
     * @return
     */
    @RequestMapping(value = "/addGoods")
    public String addGoods(@RequestParam(value = "goodsName") String goodsName,
                           @RequestParam(value = "goodsFactory") String goodsFactory,
                           @RequestParam(value = "goodsSpecs")String goodsSpecs,
                           @RequestParam(value = "goodsModel")String goodsModel,
                           ModelMap modelMap){
        Goods goods=new Goods(goodsName,goodsFactory,goodsSpecs,goodsModel);
        System.out.println(goods);
        goodsService.addGoods(goods);
        modelMap.addAttribute("Goods",goodsService.selectAllGoods());
        return "goodsList";
    }

    /**
     * 更新客户数据
     */
    //执行查询，把数据传到数据输入页面
    @RequestMapping(value = "/updateCustomerInp")
    public String updateCustomerInput(@RequestParam(value = "customeridforupdate")Integer customerid,
                                      ModelMap modelMap){
        Customer customer= customerService.selectCustomerById(customerid);
        modelMap.addAttribute("customer",customer);
        return "customerUpdate";
    }
    //数据输入完成
    @RequestMapping(value = "/updateCustomer")
    public String updateCustomer(@RequestParam(value = "customerId")Integer id,
                                 @RequestParam(value = "customerName")String customerName,
                                 @RequestParam(value = "customerTel")String customerTel,
                                 @RequestParam(value = "customerAddress")String customerAddress,
                                 @RequestParam(value = "customerTel2")String customerTel2,
                                 @RequestParam(value = "customerTransport")String customerTransport,
                                 ModelMap modelMap){
        Customer customer=new Customer(id,customerName,customerTel,customerTel2,customerAddress,customerTransport);
        customerService.updateCustomer(customer);
        System.out.println(customer);
        modelMap.addAttribute("aa",customerService.selectAllCustomer());
        return "customerList";
    }
    /**
     * 更新商品数据
     */
    @RequestMapping(value = "/updateGoodsInp")
    public String updateGoodsInput(@RequestParam(value = "goodsidforupdate")Integer goodsid,
                                   ModelMap modelMap){
        Goods goods=goodsService.selectById(goodsid);
        modelMap.addAttribute("goods",goods);
        return "goodsUpdate";
    }

    @RequestMapping(value = "/updateGoods")
    public String updateGoods(@RequestParam(value = "goodsId")Integer goodsid,
                              @RequestParam(value = "goodsName")String goodsname,
                              @RequestParam(value = "goodsFactory")String goodsfactory,
                              @RequestParam(value = "goodsModel")String goodsmodel,
                              @RequestParam(value = "goodsSpecs")String goodsspecs,
                              ModelMap modelMap){
        Goods goods=new Goods(goodsid,goodsname,goodsfactory,goodsspecs,goodsmodel);
        goodsService.update(goods);
        modelMap.addAttribute("goods",goods);
        return "goodsUpdate";
    }


    /**
     * 查询订单
     * 传入数据打包成order
     * 查询order
     * 返回数据，进入orderResult页面
     *
     */
    @RequestMapping(value = "/selectOrderResult")
    public String selectOrderResult(@RequestParam(value = "orderid")Integer orderid,
                                    @RequestParam(value = "custoemrname")String customername,
                                    @RequestParam(value = "goodsid")Integer goodsid,
                                    @RequestParam(value = "goodsname")String goodsname,
                                    @RequestParam(value = "goodsmodel")String goodsmodel,
                                    @RequestParam(value = "gppdsspecs")String goodsspecs){
//        Order order=new Order(orderid,customername,goodsid,goodsname,goodsmodel,goodsspecs);
//        List<Order> orders= orderService.selectByExample(order);

        return "selectOrderResult";
    }

    /**
     * 首次进入创建订单页
     */
    @RequestMapping(value = "/orderAddInputFirst")
    public String OrderAddInputOne(ModelMap modelMap){
        List<OrderAdd> orders=orderAddService.selectAll();
        double totalPrice=0;
        for (OrderAdd order : orders){
            totalPrice+=order.getGoodsPrice()*order.getGoodsNum();
        }
        modelMap.addAttribute("orderSumPrice",totalPrice);
        modelMap.addAttribute("orderAdds",orders);
        return "orderAddInput";
    }
    /**
     * 手动删除数据库原有数据
     * 防止上一个单子开一半人跑了
     */
    @RequestMapping(value = "/orderAddDelAll")
    public String OrderAddDelAll(ModelMap modelMap){
        return "orderAddInput";
    }
    /**
     * 创建订单
     * 进入商品数据输入页
     */
    @RequestMapping(value = "/orderAddInput")
    public String OrderAddInputOne(@RequestParam(value = "orderAddName")String orderAddName,
                                   @RequestParam(value = "orderAddFactory")String orderAddFactory,
                                   @RequestParam(value = "orderAddModel")String orderAddModel,
                                   @RequestParam(value = "orderAddSpecs")String orderAddSpecs,
                                   @RequestParam(value = "orderAddNum")double orderAddNum,
                                   @RequestParam(value = "orderAddPrice")double orderAddPrice,
                                   @RequestParam(value = "orderAddUnit")String orderAddUnit,
                                   @RequestParam(value = "orderAddRemarks")String orderAddRemarks,
                                   ModelMap modelMap){
        OrderAdd orderAdd=new OrderAdd(orderAddName,orderAddFactory,orderAddModel,orderAddSpecs,orderAddNum,orderAddPrice,orderAddUnit,orderAddRemarks);
        orderAddService.addOrderAdd(orderAdd);
        List<OrderAdd> orders=orderAddService.selectAll();
        double totalPrice=0;
        for (OrderAdd order : orders){
            totalPrice+=order.getGoodsPrice()*order.getGoodsNum();
        }
        modelMap.addAttribute("orderSumPrice",totalPrice);
        System.out.println(totalPrice);
        modelMap.addAttribute("orderAdds",orders);
        return "orderAddInput";
    }

    /**
     * 商品数据输入完成
     * 进入订单数据输入页
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/orderInformationInput")
    public String orderInformationInput(ModelMap modelMap){
        modelMap.addAttribute("orderAdds",orderAddService.selectAll());
        return "orderInformationInput";
    }
    /**
     * 订单输入数据完成
     * 调用service保存
     */
    @RequestMapping(value = "/orderAddFinal")
    public String orderAdd(@RequestParam(value = "customerName")String customerName,
                           @RequestParam(value = "transport")String transport,
                           ModelMap modelMap){
        List<Order> orders= orderService.addOrder(customerName,transport);
        System.out.println(customerName);
        System.out.println(transport);
        modelMap.addAttribute("orders",orders);
        return "orderAddSuccess";
    }
    /**
     * 销售查询（多条件查询）
     * 数据输入页的跳转
     */
    @RequestMapping(value = "/orderListSelect")
    public String orderListSelectInput(){
        return "orderListSelectInput";
    }

    /**
     * 查询功能实现，返回数据展示页
     * @param orderId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/selectOrderByExample")
    public String orderListSelect(@RequestParam(value = "orderId") Integer orderId,
                                  ModelMap modelMap){
        Order order = new Order();
        order.setOrderId(orderId);
        List<Order> orders= orderService.selectByExample(order);
        double totalPrice=0;
        for (Order order1:orders){
            totalPrice+=order1.getGoodsPrice()*order1.getGoodsNum();
        }
        modelMap.addAttribute("orderPrice",totalPrice);
        modelMap.addAttribute("orders",orders);
        return "orderListSelectOutput";

    }


}
